package com.jsyl.lwbk.web.controller.business;

import com.jsyl.lwbk.business.dto.*;
import com.jsyl.lwbk.business.service.CourseChildScoreService;
import com.jsyl.lwbk.common.core.controller.BaseController;
import com.jsyl.lwbk.common.core.domain.AjaxResult;
import com.jsyl.lwbk.common.core.page.TableDataInfo;
import com.jsyl.lwbk.common.utils.AgeUtils;
import com.jsyl.lwbk.common.utils.StringUtils;
import com.jsyl.lwbk.framework.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 训练计划
 */
@Controller
@RequestMapping("/business/courseChildScore")
public class CourseChildScoreController extends BaseController {

    private String prefix = "business/courseChildScore";

    @Autowired
    CourseChildScoreService courseChildScoreService;

    /**
     * 页面跳转
     *
     * @param mmap
     * @return
     */
    @GetMapping("/listAllChild")
    public String list(ModelMap mmap) {
        return prefix + "/childScoreList";
    }

    /**
     * 页面跳转-成绩录入
     *
     * @param mmap
     * @return
     */
    @GetMapping("/list/{childCourseId}/{scoreFlag}")
    public String list(@PathVariable("childCourseId") Long childCourseId,
                       @PathVariable("scoreFlag") Integer scoreFlag,ModelMap mmap) {
        mmap.put("childCourseId", childCourseId);
        mmap.put("scoreFlag", scoreFlag);
        return prefix + "/courseChildScoreList";
    }

    /**
     * 页面跳转-基础表现录入
     * @param childCourseId
     * @param childId
     * @param mmap
     * @return
     */
    @GetMapping("/editBasicExpression/{childCourseId}/{childId}")
    public String editBasicExpression(@PathVariable("childCourseId") String childCourseId,
                                      @PathVariable("childId")Long childId, ModelMap mmap) {
        mmap.put("childCourseId", childCourseId);
        mmap.put("childId", childId);
        return prefix + "/editBasicExpression";
    }
    /**
     * 页面跳转-成绩详情
     * @param mmap
     * @return
     */
    @GetMapping("/childScoreDetail/{childId}/{childName}/{age}/{grade}")
    public String childScoreDetail(@PathVariable("childId")Long childId,
                                   @PathVariable("childName")String childName,
                                   @PathVariable("age")String age,
                                   @PathVariable("grade")String grade,ModelMap mmap) {
        mmap.put("childId", childId);
        mmap.put("childInfo", childName+" 年龄："+age+"     年级："+ AgeUtils.getGrade(Integer.valueOf(grade)));
        return prefix + "/childScoreDetail";
    }
    /**
     * 页面跳转-历史成绩
     * @param mmap
     * @return
     */
    @GetMapping("/childScoreHistory/{childId}/{projectCode}")
    public String childScoreHistory(@PathVariable("childId")Long childId,@PathVariable("projectCode")String projectCode, ModelMap mmap) {
        mmap.put("childId", childId);
        mmap.put("projectCode", projectCode);
        return prefix + "/childScoreHistory";
    }
    /**
     *成绩详情
     *
     * @return
     */
    @PostMapping("/childScoreDetailList")
    @ResponseBody
    public TableDataInfo childScoreDetailList(ChildScoreDetailDTO detailDTO) {
        startPage();
        if(StringUtils.isNotEmpty(detailDTO.getDateRange())){
            detailDTO.setStartDate(detailDTO.getDateRange().split(" - ")[0]);
            detailDTO.setEndDate(detailDTO.getDateRange().split(" - ")[1]);
        }
        List<ChildScoreDetailDTO> list = courseChildScoreService.childScoreDetailList(detailDTO.getChildId(),detailDTO.getTeacherName(),detailDTO.getStartDate(),detailDTO.getEndDate());
        return getDataTable(list);
    }

    /**
     *历史成绩
     *
     * @return
     */
    @GetMapping("/listHistoryScore")
    @ResponseBody
    public AjaxResult listHistoryScore(@RequestParam("childId")Long childId,@RequestParam("projectCode")String projectCode) {
        List<HistoryScoreDTO> list = courseChildScoreService.listHistoryScore(childId,projectCode);
        return AjaxResult.success(list);
    }

    /**
     *备课主页数据
     *
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult findList(@RequestParam("childCourseId")Long childCourseId) {
        List<CourseChildScoreDTO> list = courseChildScoreService.list(childCourseId);
        return success(list);
    }
    /**
     * 历史基础表现
     *
     * @return
     */
    @PostMapping("/listBasicExpression")
    @ResponseBody
    public TableDataInfo listBasicExpression(Long childCourseId,Long childId) {
        startPage();
        List<BasicExpressionDTO> list = courseChildScoreService.listBasicExpression(childCourseId,childId);
        return getDataTable(list);
    }

    /**
     *
     * @param childCourseId
     * @param childId
     * @return
     */
    @GetMapping("/getBasicExpression")
    @ResponseBody
    public AjaxResult getBasicExpression(@RequestParam("childCourseId") Long childCourseId,@RequestParam("childId") Long childId) {
        return success(this.courseChildScoreService.getBasicExpression(childCourseId,childId));
    }


    /**
     * 保存基础表现
     *
     * @param dto
     * @return
     */
    @PostMapping("/saveBasicExpression")
    @ResponseBody
    public AjaxResult saveBasicExpression(@RequestBody SaveBasicExpressionDTO dto) {
        dto.setUpdateUser(ShiroUtils.getLoginName());
//        this.courseChildScoreService.saveBasicExpression(dto);
        this.courseChildScoreService.saveChildBasicExpression(dto);
        return success();
    }

    /**
     * 保存成绩
     *
     * @param dto
     * @return
     */
    @PostMapping("/saveScore")
    @ResponseBody
    public AjaxResult saveScore(@RequestBody SaveScoreDTO dto) {
        dto.setUpdateUser(ShiroUtils.getLoginName());
        this.courseChildScoreService.saveScore(dto);
        return success();
    }


    /**
     *学员成绩主页数据
     *
     * @return
     */
    @PostMapping("/listAllChild")
    @ResponseBody
    public TableDataInfo listAllChild(ChildScoreDTO dto) {
        startPage();
        if(ShiroUtils.isTeacher()){
            dto.setTeacherId(ShiroUtils.getUserId());
        }
        List<ChildScoreDTO> list = courseChildScoreService.listAllChild(dto);
        return getDataTable(list);
    }








}
